import { quit } from "@/common"
import axios from "axios"
import { ElMessage } from "element-plus"
import { config } from "process"
const instance = axios.create({
    baseURL: "/api"
})
instance.interceptors.request.use(
    (config) => {
        let token = localStorage.getItem("token")
        if (token) {
            if (config.headers) {
                config.headers["Authorization"] = token
            }
        }
        return config
    },
    (err) => Promise.reject(err)
)
instance.interceptors.response.use(
    (response) => {
        const data = response.data as IResponse<any>
        if (data.code == 401) {
            quit(undefined, () => {
                // 提示
                ElMessage.error("token过期了，请重新登录")
            })

        }
        else if (data.code !== 200) {
            ElMessage.error(data.message)
        }
        return response.data
    },
    (err) => {
        ElMessage.error("请求失败，这是错误信息：" + err.message)
        Promise.reject(err)
    }
)
export default instance;